<template>
  <div class="container">
    <el-dialog :title="title" :visible="dialogVisible" width="25%" @close="close" @open="open">
      <el-form :inline="true" :rules="rules" :model="ruleForm" ref="resForm">
        <el-form-item label="所属学科" prop="subjectID" v-if="!rowTagId">
          <el-select placeholder="请选择" v-model="ruleForm.subjectID">

            <el-option v-for="item in labelData" :key="item.value" :label="item.label" :value="item.value"></el-option>

          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" prop="directoryName">
          <el-input placeholder="请输入目录名称" v-model="ruleForm.directoryName" style="width: 225px"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="admin">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { add, update } from '@/api/hmmm/directorys'
import { simple } from '@/api/hmmm/subjects'
export default {
  name: 'tagsadd',
  props: {
    dialogVisible: {
      typy: Boolean,
      default: false
    },
    title: {
      typy: String,
      default: ''
    },
    rowTagId: {
      typy: Number,
      default: null
    },
    rowtagName: {
      typy: String,
      default: ''
    }

  },
  data () {
    return {
      ruleForm: {
        id: 0,
        subjectID: null, // 一开始赋值为空，否则弹出框显示数据
        directoryName: ''
      },
      value: '',
      labelData: [], // 学科简单信息
      rules: {
        subjectID: [
          { required: true, message: '请选择所属学科', trigger: ['change', 'blur'] }
        ],
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' }
        ]
      }

    }
  },
  methods: {

    close () {
      this.$emit('update:dialogVisible', false)
      this.ruleForm = {
        subjectID: null,
        directoryName: '',
        id: 0
      }
      this.$refs.resForm.resetFields()
    },
    async open () {
      // 获取学科简单信息
      const { data } = await simple()
      // console.log(data)
      this.labelData = data
      if (this.$parent.editID) { // 修改的话才让他回显
        this.ruleForm = this.$parent.nodeData
        this.ruleForm.id = this.$parent.editID
      }
    },
    async admin () {
      this.$refs.resForm.validate(async flag => {
        if (!flag) return
        if (this.title === '修改目录') {
          const res = await update(this.ruleForm)// 把数据传过去
          console.log(res)
        } else if (this.title === '新增目录') {
          if (this.rowTagId && this.rowtagName) {
            await add({ subjectID: Number(this.rowTagId), directoryName: this.ruleForm.directoryName })
          } else {
            await add(this.ruleForm)
          }
          this.$message.success('添加成功')
        }

        // this.$message.success('添加成功')
        this.close()
        this.$parent.listData()
      })
    }
  }
}
</script>

<style scoped lang='less'>

</style>
